package com.student.xujiang.myallstudent;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.student.xujiang.R;
import com.student.xujiang.myallstudent.GsonAndJackson.JacksonAndGsonMainActivity;
import com.student.xujiang.myallstudent.Rxlifecycle.BaseRxLifecycleActivity;
import com.student.xujiang.myallstudent.Rxlifecycle.RxlifecycleMainActivity;
import com.student.xujiang.myallstudent.eventbus.EventBusMainActivity;
import com.student.xujiang.myallstudent.glide.GlideMainActivity;
import com.student.xujiang.myallstudent.mrecyclerview.RecyclerMainActivity;
import com.student.xujiang.myallstudent.okhttp3.OkHttp3MainActivity;
import com.student.xujiang.myallstudent.retrofit2.Retrofit2MainActivity;
import com.student.xujiang.myallstudent.rxjava.RxJava1MainActivity;
import com.student.xujiang.myallstudent.rxjava.rxjava2.RxJava2MainActivity;
import com.student.xujiang.mysmalluse.companylogin.retrofit2protogenesis.CLoginMainActivity;

/**
 * 作者：xujiang on 2017/3/18 15:55 Created
 * 邮箱：xujiang5@163.com
 */
public class MainAdapter extends RecyclerView.Adapter {

        //调用adapter的引用
        private MainActivity mainActivity;

        private String[] mData;



        //构造方法，不知道context可不可以
        public MainAdapter(MainActivity mainActivity, String[] mData) {
            this.mainActivity = mainActivity;
            this.mData = mData;
        }

        //需要一个内部类，继承ViewHolder
        class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

            private int position;

            private View itemView;

            //get方法暴露出控件以便调用
            public TextView getList_name() {
                return list_name;
            }

            //这下面是item里的控件，这里只有一个，所以就写了一个
            private TextView list_name;

            //构造方法传入一个View作为根 有时命名为root
            public ViewHolder(View itemView) {
                super(itemView);
                this.itemView = itemView;
                initBindView(itemView);

            }

            private void initBindView(View root) {
                //在此处对item里的控件与上面定义的控件进行绑定链接
                list_name = (TextView) root.findViewById(R.id.main_list_item_tv);

            }

            private void bind(int position){
                this.position =position;
                itemView.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                switch (position){
                    case 0:
                        Log.i("MainAdapter","第一个点击测试");
                        Intent intent = new Intent(mainActivity, RecyclerMainActivity.class);
                        mainActivity.startActivity(intent);
                        break;
                    case 1:
                        Intent intent0 = new Intent(mainActivity, GlideMainActivity.class);
                        mainActivity.startActivity(intent0);
                        break;

                    case 2:
                        Intent intent1 = new Intent(mainActivity, OkHttp3MainActivity.class);
                        mainActivity.startActivity(intent1);
                        break;

                    case 3:
                        Intent intent2 = new Intent(mainActivity, EventBusMainActivity.class);
                        mainActivity.startActivity(intent2);
                        break;

                    case 4:
                        Intent intent3 = new Intent(mainActivity, Retrofit2MainActivity.class);
                        mainActivity.startActivity(intent3);
                        break;
                    case 5:
                        Intent intent4 = new Intent(mainActivity, RxJava1MainActivity.class);
                        mainActivity.startActivity(intent4);
                        break;

                    case 6:
                        Intent intent5 = new Intent(mainActivity, JacksonAndGsonMainActivity.class);
                        mainActivity.startActivity(intent5);
                        break;

                    case 7:
                        Intent intent6 = new Intent(mainActivity, CLoginMainActivity.class);
                        mainActivity.startActivity(intent6);
                        break;
                    case 8:
                        Intent intent7 = new Intent(mainActivity, RxlifecycleMainActivity  .class);
                        mainActivity.startActivity(intent7);
                        break;

                    case 9:
                        Intent intent8 = new Intent(mainActivity, RxJava2MainActivity.class);
                        mainActivity.startActivity(intent8);


                }
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            //http://blog.csdn.net/qq_24229973/article/details/52980743 居中问题
            return new ViewHolder(LayoutInflater.from(mainActivity).inflate(R.layout.item_main_list, parent ,false));
            //或者
            //return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_list, parent ,false));

        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder vh = (ViewHolder) holder;

            vh.bind(position);

            String car = mData[position];

            vh.getList_name().setText(car);


        }

        @Override
        public int getItemCount() {

            return mData.length;
        }


    }
